<template>
	<view class="message">
		<view class="head" :style="{height: pHeight + tHeight + 'px'}">
			<view class="xztitle" :style="[{height: pHeight + 'px', top: tHeight + 'px',lineHeight: pHeight + 'px'}]">
				门店地图
				<view class="iconfont icon-dayuhao1" @click="fanhui"></view>
			</view>
		</view>
		<view class="headtop" :style="{marginTop: pHeight + tHeight + 'px'}">
			<view class="top">
				<view class="left clearfix">
					<view class="mess fl" @click="qiehuan(1)">
						<view :class="[attrIndex==1?'active':'']">直营门店</view>
						<text class="dian" v-if="attrIndex == 1"></text>
					</view>
					<view class="mess mess1 fl" @click="qiehuan(2)">
						<view :class="[attrIndex==2?'active':'']">代理门店</view>
						<text class="dian" v-if="attrIndex == 2"></text>
					</view>
					<view class="mess mess1 fl" @click="qiehuan(3)">
						<view :class="[attrIndex==3?'active':'']">全部门店</view>
						<text class="dian" v-if="attrIndex == 3"></text>
					</view>
				</view>
			</view>
			<view class="form">
				<view class="content clearfix">
					<view class="name fl">创建门店时间</view>
					<view class="xuanze fr" @click="show = true">
						{{date}}
					</view>
					<u-calendar v-model="show" :mode="mode" toolTip="时间选择" @change="change"></u-calendar>
				</view>
				<view class="sousuo clearfix">
					<input type="text" placeholder="请输入关键字" v-model="keywords" @confirm="sousuo" />
					<image :src="baseUrl + '/wximage/sousuo.png'" @click="sousuo"></image>
				</view>
			</view>
		</view>
		<map id="map" :longitude="longitude" :latitude="latitude" scale="14" class="map" :markers="markers" @markertap="markertap"></map>
		<view class="" style="position: absolute;z-index: 200;bottom: 100rpx;" v-if="stationpan">
			<view class="" style="height: 200rpx;width: 750rpx;display: flex; justify-content: center;align-items: center; ">
				<view class="" style="width: 90%;height: 180rpx; background-color: #fff;border-radius: 20rpx;">
					<view class="" style="height: 100%; display: flex;align-items: center;">
						<view class="" style="height: 150rpx;margin-left: 15rpx;">
							<image style="height: 150rpx;width: 150rpx;" :src="stationInfo.sitepicture" mode=""></image>
						</view>
						<view class="" style="width: 350rpx;margin-left: 15rpx;">
							<view class="" style="font-size: 27rpx;">
								{{stationInfo.name}}
							</view>
							<view class="" style="font-size: 23rpx;">
								{{stationInfo.full_address}}
							</view>
						</view>
						<view class="">
							<view class="" @click="navigation" style="height: 60rpx;width: 100rpx; font-size: 27rpx;border: solid 1px #368BF8;display: flex;justify-content: center;align-items: center;border-radius: 20rpx;">
								导航
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.baseUrl,
				headerTop: 0,
				pHeight: 0,
				tHeight: 0,
				attrIndex: 1,
				keywords: '',
				showstatus: false,
				timelist: [{
					value: '0',
					label: '全部',
				}, {
					value: '1',
					label: '合作中',
				}, {
					value: '2',
					label: '合作结束',
				}],
				value: '',
				label: '请选择',
				show: false,
				mode: 'range',
				sdate: '',
				edate: '',
				date: '请选择',
				longitude: '',
				latitude: '',
				markers: [],
				map_image: '',
				stationpan: false,
				stationInfo: {}
			}
		},
		onLoad() {
			// 头部距离
			let res = uni.getSystemInfoSync() ///微信api方法
			let titleH;
			if (res && res['system']) {
				// 判断是否是安卓操作系统 （标题栏苹果为44px,安卓为48px）
				if (res['system'].indexOf('Android') > 0) {
					titleH = 48
				} else {
					titleH = 46
				}
				this.headerTop = titleH + res['statusBarHeight']
				this.pHeight = titleH
				this.tHeight = res['statusBarHeight']
			}
			
			//获取基本配置信息
			this.request({
				url: '/xcx/webconfig',
				data: {}
			}).then(res => {
				if (res.data.code == 200) {
					this.map_image = res.data.data.map_image;
				} else {
					uni.showToast({
						title: '请求失败，请稍后再试',
						icon: 'none'
					})
				}
			})
			
			this.getCurrentCity();//当前位置信息
			
			this.getList(this.sdate,this.edate,this.value,this.keywords,this.attrIndex);
		},
		methods: {
			navigation(){
				const latitude = Number(this.stationInfo.lat);
				const longitude = Number(this.stationInfo.lng);
				const name = this.stationInfo.name;
				const address = this.stationInfo.full_address;
				uni.getLocation({
					success(res) {
						uni.openLocation({
							name: name,
							address: address,
							latitude: latitude,
							longitude: longitude,
							scale: 15
						});
					}
				});
			},
			markertap(e){
				this.request({
					url: '/agent/stationinfo',
					data: {
						id: e.markerId,
					},
					method: "POST"
				}).then(res => {
					this.stationpan = true;
					this.stationInfo = res.data.data;
				})
			},
			getList(sdate,edate,value,keywords,attrIndex){
				const points = [];
				this.markers = [];
				
				this.request({
					url: '/xcx/storemap',
					data: {
						sdate: sdate,
						edate: edate,
						value: '',
						keywords: keywords,
						attr: attrIndex,
						agent_id: uni.getStorageSync('agent_id'),
						role_id: uni.getStorageSync('role_id'),
					},
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						let list = res.data.data;
				
						for (let i = 0; i < list.length; i++) {
							points.push({
								id: list[i].id,
								latitude: list[i].lat,
								longitude: list[i].lng,
								title: list[i].name,
							})
						}
						
						points.forEach((point, index) => {
							this.markers.push({
								id: point.id,
								latitude: point.latitude,
								longitude: point.longitude,
								width: 20,
								height: 20,
								title: point.title,
								iconPath: this.map_image, // 标注的图标地址
							});
						});
					}
				})
			},
			confirm(e) {
				this.value = e[0].value;
				this.label = e[0].label;
				
				this.getList(this.sdate,this.edate,this.value,this.keywords,this.attrIndex);
			},
			change(e) {
				this.sdate = e.startDate;
				this.edate = e.endDate;
				
				this.date = this.sdate + '至' + this.edate;
				
				this.getList(this.sdate,this.edate,this.value,this.keywords,this.attrIndex);
			},
			sousuo(){
				this.getList(this.sdate,this.edate,this.value,this.keywords,this.attrIndex);
			},
			qiehuan(index){
				this.attrIndex = index;
				this.sdate = '';
				this.edate = '',
				this.value = '',
				this.keywords = '',
				this.label = '请选择';
				this.date = '请选择';
				
				this.getList(this.sdate,this.edate,this.value,this.keywords,this.attrIndex);
			},
			getCurrentCity() {
				// #ifdef MP-ALIPAY
				my.getLocation({
					type: 0,
					success: (res) => {
						this.latitude = res.latitude;
						this.longitude = res.longitude;
			
						uni.setStorageSync('latitude', this.latitude);
						uni.setStorageSync('longitude', this.longitude);
					},
					fail: () => {
						uni.showToast({
							title: '无法获取位置信息',
							icon: 'none'
						});
					}
				});
				// #endif
			
				// #ifdef MP-WEIXIN
				uni.getLocation({
					type: 'wgs84',
					success: (res) => {
						this.latitude = res.latitude;
						this.longitude = res.longitude;
			
						uni.setStorageSync('latitude', this.latitude);
						uni.setStorageSync('longitude', this.longitude);
					},
					fail: () => {
						uni.showToast({
							title: '无法获取位置信息',
							icon: 'none'
						});
					}
				});
				// #endif
			},
			fanhui() {
				uni.navigateBack();
			},
			topage(url) {
				uni.navigateTo({
					url
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.message {
		width: 100%;
		margin: 0 auto;
		height: 100vh;
		background: #f9f9ff;
		overflow-y: scroll;

		.head {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);

			.xztitle {
				position: fixed;
				left: 0%;
				top: 0;
				width: 100%;
				text-align: left;
				color: #333333;
				font-size: 32rpx;
				padding-left: 74rpx;
				font-weight: 700;

				text {
					font-size: 24rpx;
				}

				.iconfont {
					position: absolute;
					top: 0;
					left: 30rpx;
					font-size: 44rpx;
				}
			}
		}

		.headtop {
			width: 100%;
			padding: 20rpx 40rpx 0;
			box-sizing: border-box;
			position: relative;
			z-index: 16;
			
			.left {
				.mess {
					height: 48rpx;
					line-height: 48rpx;
					text-align: center;
			
					view {
						font-size: 24rpx;
						color: #333333;
					}
			
					.dian {
						width: 8rpx;
						height: 8rpx;
						background: #5C88FA;
						display: block;
						border-radius: 50%;
						margin: 0 auto;
					}
			
					.active {
						font-size: 32rpx;
						color: #333333;
						font-weight: 700;
						margin-left: 0;
					}
				}
			
				.mess1 {
					margin-left: 20rpx;
				}
			}
			
			.form{
				width: 100%;
				margin: 0 auto;
				margin-top: 20rpx;
				background: rgba(255, 255, 255, 0.6);
				border-radius: 20rpx;
				padding: 0 20rpx 20rpx 20rpx;
				box-shadow:rgba(223, 230, 255, 0.67) 0px 0px 6px;
				
				.content{
					height: 92rpx;
					line-height: 92rpx;
					border-bottom: 1rpx solid #D8D8D8;
					
					.name{
						font-size: 28rpx;
						color: #333333;
						width: 40%;
					}

					.xuanze{
						width: 60%;
						text-align: right;
						height: 92rpx;
						line-height: 92rpx;
						color: #368BF8;
					}
				}
			}
			
			.sousuo {
				width: 100%;
				box-sizing: border-box;
				position: relative;
				margin-top: 20rpx;

				input {
					width: 100%;
					height: 60rpx;
					line-height: 60rpx;
					background: #ffffff;
					border-radius: 30rpx;
					padding-left: 30rpx;
					color: #999999;
					box-sizing: border-box;
				}

				image {
					width: 72rpx;
					height: 40rpx;
					position: absolute;
					right: 10rpx;
					top: 10rpx;
				}
			}
		}
		
		.map{
			width: 100%;
			height: 100vh;
			position: absolute;
			top: 0;
			left: 0;
			z-index: 10;
		}
	}
</style>